#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SeqListPrint(SL* sl)
{
	int i = 0;
	for (i = 0; i < sl->size; i++)
	{
		printf("%d ", sl->ps[i]);
	}
	printf("\n");
}

void SeqListInitial(SL* sl)
{
	sl->ps = NULL;
	sl->capacity = sl->size = 0;
}

void SeqListPushBack(SL* sl, SLType x)
{
	if (sl->capacity == sl->size)
	{
		int newcapacity = (sl->capacity == 0) ? 4 : (sl->capacity * 4);
		SLType* t = (SLType*)realloc(sl->ps, newcapacity * sizeof(SLType));
		if (t == NULL)
		{
			perror("initial-NULL");
			exit(EXIT_FAILURE);
		}
		sl->ps = t;
		sl->capacity = newcapacity;
	}
	sl->ps[sl->size] = x;
	sl->size++;
}

void SeqListPopBack(SL* sl)
{
	if (sl->size > 0)
		sl->size--;
}

void SeqListPushFront(SL* sl, SLType x)
{
	if (sl->capacity == sl->size)
	{
		int newcapacity = (sl->capacity == 0) ? 4 : (sl->capacity * 4);
		SLType* t = (SLType*)realloc(sl->ps, newcapacity * sizeof(SLType));
		if (t == NULL)
		{
			perror("initial-NULL");
			exit(EXIT_FAILURE);
		}
		else
		{
			sl->ps = t;
			sl->capacity = newcapacity;
		}
	}
	int end = sl->size-1;
	while (end > 0)
	{
		sl->ps[end] = sl->ps[end - 1];
		end--;
	}
	sl->ps[0] = x;
	sl->size++;
}

void SeqListPopFront(SL* sl)
{
	if (sl->size > 0)
	{
		int begin = 0;
		for (begin = 0; begin < sl->size - 1; begin++)
			sl->ps[begin] = sl->ps[begin + 1];
		sl->size--;
	}
}

void SeqListDestroy(SL* sl)
{
	free(sl->ps);
	sl->ps = NULL;
	sl->capacity = sl->size = 0;
}

void SeqListInsert(SL* sl, int pos, SLType x)
{
	if (sl->capacity == sl->size)
	{
		int newcapacity = (sl->capacity == 0) ? 4 : (sl->capacity * 4);
		SLType* t = (SLType*)realloc(sl->ps, newcapacity * sizeof(SLType));
		if (t == NULL)
		{
			perror("initial-NULL");
			exit(EXIT_FAILURE);
		}
		else
		{
			sl->ps = t;
			sl->capacity = newcapacity;
		}
	}
	int end = sl->size - 1;
	while (end > pos)
	{
		sl->ps[end] = sl->ps[end - 1];
		end--;
	}
	sl->ps[pos] = x;
	sl->size++;
}

void SeqListErase(SL* sl, int pos)
{
	if (sl->size > 0)
	{
		int begin = pos;
		for (begin = pos; begin < sl->size - 1; begin++)
			sl->ps[begin] = sl->ps[begin + 1];
		sl->size--;
	}
}